package com.example.work_one

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView

class GoodsAdapter(val goodslist:List<Goods>): RecyclerView.Adapter<GoodsAdapter.ViewHolder>(){

    inner class ViewHolder(view:View) : RecyclerView.ViewHolder(view) {
        val goodsImage: ImageView = view.findViewById(R.id.shoppingImage)
        val goodName: TextView = view.findViewById(R.id.shoppingName)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view=LayoutInflater.from(parent.context)
            .inflate(R.layout.shopping_item,parent,false)
        val viewHolder=ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position=viewHolder.adapterPosition
            val goods=goodslist[position]
            Toast.makeText(parent.context,"this is ${goods.name}",Toast.LENGTH_SHORT).show()
        }

        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
       val good=goodslist[position]
        holder.goodsImage.setImageResource(good.imageId)
        holder.goodName.text=good.name
    }

    override fun getItemCount()=goodslist.size


}